package com.june.junemplayer.utils;

import android.annotation.SuppressLint;
import android.content.ContentResolver;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.util.Log;


import com.june.junemplayer.pojo.Song;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class DataConversionUtil {

    public static ArrayList<Uri> Intent2Uri(Intent data) {
        ArrayList<Uri> uris = new ArrayList<>();
        if (data.getClipData() == null) {
            Log.d("June", "2");

            Uri uri = Uri.parse(Uri.decode(data.getData().toString()));
            uris.add(uri);
        } else {
            Log.d("June", "3");
            for (int i = 0; i < data.getClipData().getItemCount(); i++) {
                Uri uri = Uri.parse(Uri.decode(data.getClipData().getItemAt(i).getUri().toString()));
                uris.add(uri);
            }
        }
        return uris;
    }

    public static ArrayList<String> Intent2Path(Intent data) {
        ArrayList<String> paths = new ArrayList<>();
        if (data.getClipData() == null) {
            Log.d("June", "2");
            String path = Uri.decode(data.getData().getPath().replace("/document/raw:", ""));
            paths.add(path);
        } else {
            Log.d("June", "3");
            for (int i = 0; i < data.getClipData().getItemCount(); i++) {
                String path = Uri.decode(data.getClipData().getItemAt(i).getUri().getPath().replace("/document/raw:", ""));
                paths.add(path);
            }
        }
        return paths;
    }

    public static ArrayList<Song> Uri2Song(ArrayList<Uri> uris) {
        ArrayList<Song> songs = new ArrayList<>();
        String[] fileName;
        String[] fileNames;
        for (int i = 0; i < uris.size(); i++) {
            String path = Uri.decode(uris.get(i).getPath().replace("/document/raw:", ""));
            Log.d("June", path);
            fileName = path.split("/");
            Log.d("June", path);
            fileNames = fileName[fileName.length - 1].split(" - ");
            Song song = new Song();
            song.setTitle((fileNames[1].split("\\.")[0]) == null ? "unknown" : fileNames[1].split("\\.")[0]);
            song.setArtist(fileNames[0] == null ? "佚名" : fileNames[0]);
            song.setPath(path);
            song.setDuration(-1);
            song.setSize(-1L);
            song.setLyric(-1L);
            song.setExtra("Block");
            songs.add(song);
        }
        return songs;
    }

    public static ArrayList<Song> Path2Song(ArrayList<String> paths) {
        ArrayList<Song> songs = new ArrayList<>();
        String[] fileName;
        String[] fileNames;
        for (int i = 0; i < paths.size(); i++) {
            fileName = paths.get(i).split("/");
            fileNames = fileName[fileName.length - 1].split(" - ");
            Song song = new Song();
            song.setTitle((fileNames[1].split("\\.")[0]) == null ? "unknown" : fileNames[1].split("\\.")[0]);
            song.setArtist(fileNames[0] == null ? "佚名" : fileNames[0]);
            song.setPath(paths.get(i));
            song.setDuration(-1);
            song.setSize(-1L);
            song.setLyric(-1L);
            song.setExtra("Block");
            songs.add(song);
        }
        return songs;
    }

    /**
     * 扫描系统里面的音频文件，返回一个list集合
     */
    public static  ArrayList<Song> getSongsFromProvider(Context context) {
        Log.d("JuneProvider","----Provider获取本地歌曲----");
        ArrayList<Song> songs = new ArrayList<>();
        Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null,
                null, MediaStore.Audio.Media.IS_MUSIC);
        int count=0;
        if (cursor.moveToLast()) {
            do {
                Song song = new Song();
                song.setTitle(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE)));
                song.setArtist(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST)));
                song.setPath(cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)));
                song.setDuration(cursor.getInt(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DURATION)));
                song.setSize(cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.SIZE)));
                Log.d("JuneProvider", count + "----" + song);
                count++;
                songs.add(song);
            } while (cursor.moveToPrevious());
        }
        cursor.close();
        Log.d("JuneProvider", "----Provider----获取到歌曲  "+count+"  条----");
        return songs;
    }
}